package jmenu.main;

import java.io.File;
import javax.xml.transform.TransformerException;
import org.w3c.dom.*;
import javax.xml.xpath.*;
import javax.xml.parsers.*;
import java.io.IOException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.SAXException;

public class Settings {

    private String file = System.getProperty("user.home") + "\\Local Settings\\jmenu\\";

    private int locX;
    private int locY;
    private boolean showPanel;

    private int program;
    
    private double [][] rateMeal;

    private int aditB;
    private int aditT;
    private int aditS;
    private int aditCh;


    public Settings()
    {
        this.locX = 100;
        this.locY = 100;
        this.showPanel = true;

        this.program = FitnessProgram.SLIMMING;

        this.aditB = 0;
        this.aditT = 0;
        this.aditS = 0;
        this.aditCh = 0;
    }

    public void setLocX(int x)
    {
         if (x > 0)
            this.locX = x;
        else
            this.locX = 0;
    }

    public int getLocX()
    {
        return this.locX;
    }

    public void setLocY(int y)
    {
        if (y > 0)
            this.locY = y;
        else
            this.locY = 0;
    }

    public int getLocY()
    {
        return this.locY;
    }

    public void setShowPanel(boolean  p)
    {
        this.showPanel = p;
    }

    public boolean  getShowPanel()
    {
        return this.showPanel;
    }

    public void setProgram(int p)
    {
        switch (p)
        {
            case 0:
            case 1:
                this.program = p;
            break;
            default:
                this.program = 0;
            break;
        }
    }

    public int getProgram()
    {
        return this.program;
    }

    public void setAditB(int y)
    {
        this.aditB = y;
    }

    public int getAditB()
    {
        return this.aditB;
    }

    public void setAditT(int y)
    {
        this.aditT = y;
    }

    public int getAditT()
    {
        return this.aditT;
    }

    public void setAditS(int y)
    {
        this.aditS = y;
    }

    public int getAditS()
    {
        return this.aditS;
    }

    public void setAditCh(int y)
    {
        this.aditCh = y;
    }

    public int getAditCh()
    {
        return this.aditCh;
    }

    public void load()
    {
        try
        {
            DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            domFactory.setNamespaceAware(true);
            DocumentBuilder builder = domFactory.newDocumentBuilder();

            Document doc = builder.parse(this.file+"settings.xml");
            XPath xpath = XPathFactory.newInstance().newXPath();

            XPathExpression expr;
            Object result;
            NodeList node;
            //load locX
            expr = xpath.compile("//jmenu/settings/locx/text()");
            result = expr.evaluate(doc, XPathConstants.NODESET);
            node = (NodeList) result;
            if (node.getLength() == 1)
                try
                {
                    this.locX = Integer.valueOf(node.item(0).getNodeValue());
                }catch (NumberFormatException ex) {}
            //load locY
            expr = xpath.compile("//jmenu/settings/locy/text()");
            result = expr.evaluate(doc, XPathConstants.NODESET);
            node = (NodeList) result;
            if (node.getLength() == 1)
                try
                {
                    this.locY = Integer.valueOf(node.item(0).getNodeValue());
                }catch (NumberFormatException ex) {}
            //load showPanel
            expr = xpath.compile("//jmenu/settings/showpanel/text()");
            result = expr.evaluate(doc, XPathConstants.NODESET);
            node = (NodeList) result;
            if (node.getLength() == 1)
                    this.showPanel = Boolean.valueOf(node.item(0).getNodeValue());
            //load fitnessprogram
            expr = xpath.compile("//jmenu/settings/fitnessprogram/text()");
            result = expr.evaluate(doc, XPathConstants.NODESET);
            node = (NodeList) result;
            if (node.getLength() == 1)
                try
                {
                    this.program = Integer.valueOf(node.item(0).getNodeValue());
                }catch (NumberFormatException ex) {}

            //load aditB
            expr = xpath.compile("//jmenu/settings/aditB/text()");
            result = expr.evaluate(doc, XPathConstants.NODESET);
            node = (NodeList) result;
            if (node.getLength() == 1)
                try
                {
                    this.aditB = Integer.valueOf(node.item(0).getNodeValue());
                }catch (NumberFormatException ex) {}

            //load aditS
            expr = xpath.compile("//jmenu/settings/aditS/text()");
            result = expr.evaluate(doc, XPathConstants.NODESET);
            node = (NodeList) result;
            if (node.getLength() == 1)
                try
                {
                    this.aditS = Integer.valueOf(node.item(0).getNodeValue());
                }catch (NumberFormatException ex) {}

            //load aditT
            expr = xpath.compile("//jmenu/settings/aditT/text()");
            result = expr.evaluate(doc, XPathConstants.NODESET);
            node = (NodeList) result;
            if (node.getLength() == 1)
                try
                {
                    this.aditT = Integer.valueOf(node.item(0).getNodeValue());
                }catch (NumberFormatException ex) {}

            //load aditCh
            expr = xpath.compile("//jmenu/settings/aditCh/text()");
            result = expr.evaluate(doc, XPathConstants.NODESET);
            node = (NodeList) result;
            if (node.getLength() == 1)
                try
                {
                    this.aditCh = Integer.valueOf(node.item(0).getNodeValue());
                }catch (NumberFormatException ex) {}


        }
        catch (IOException ex)
        {return;}
        catch (ParserConfigurationException ex)
        {return;}
        catch (SAXException ex)
        {return;}
        catch (XPathExpressionException ex)
        {return;}
    }

    public void save()
    {
        if (!(new File(this.file)).exists())
        {
           (new File(this.file)).mkdir();
        }

        try
        {
            DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            domFactory.setNamespaceAware(true);
            DocumentBuilder builder = domFactory.newDocumentBuilder();

            Document document = builder.newDocument();

            Element settings = document.createElement("jmenu");
            document.appendChild(settings);


            Element jmenu = document.createElement("settings");

            Element locx = document.createElement("locx");
            locx.appendChild(document.createTextNode(String.valueOf(this.getLocX())));
            jmenu.appendChild(locx);

            Element locy = document.createElement("locy");
            locy.appendChild(document.createTextNode(String.valueOf(this.getLocY())));
            jmenu.appendChild(locy);

            Element showpanel = document.createElement("showpanel");
            showpanel.appendChild(document.createTextNode(String.valueOf(this.getShowPanel())));
            jmenu.appendChild(showpanel);

            Element program = document.createElement("fitnessprogram");
            program.appendChild(document.createTextNode(String.valueOf(this.getProgram())));
            jmenu.appendChild(program);

            Element aditB = document.createElement("aditB");
            aditB.appendChild(document.createTextNode(String.valueOf(this.getAditB())));
            jmenu.appendChild(aditB);

            Element aditT = document.createElement("aditT");
            aditT.appendChild(document.createTextNode(String.valueOf(this.getAditT())));
            jmenu.appendChild(aditT);

            Element aditS = document.createElement("aditS");
            aditS.appendChild(document.createTextNode(String.valueOf(this.getAditS())));
            jmenu.appendChild(aditS);

            Element aditCh = document.createElement("aditCh");
            aditCh.appendChild(document.createTextNode(String.valueOf(this.getAditCh())));
            jmenu.appendChild(aditCh);

            settings.appendChild(jmenu);

            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();

            DOMSource source = new DOMSource(document);
            StreamResult result = new StreamResult(new File(this.file+"settings.xml"));

            transformer.transform(source, result);
        }
        catch (ParserConfigurationException ex){}
        catch (TransformerException ex){}
    }

}